package com.aaa.service.impl;

import com.aaa.entity.FrontUser;
import com.aaa.mapper.CommentMapper;
import com.aaa.mapper.CommentPraiseMapper;
import com.aaa.mapper.FrontUserMapper;
import com.aaa.mapper.PraiseMapper;
import com.aaa.service.IFrontUserService;
import com.aaa.util.secl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Bean
 * @since 2021-09-02
 */
@Service
@Slf4j
public class FrontUserServiceImpl extends ServiceImpl<FrontUserMapper, FrontUser> implements IFrontUserService {

    @Autowired
    FrontUserMapper frontUserMapper;
    @Autowired
    PraiseMapper praiseMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    CommentPraiseMapper commentPraiseMapper;
    @Autowired
    secl secl;


    public List<Map<String,Object>> login(String username,String password){
        return frontUserMapper.login(username,password);
    }
    public Integer add(String uname,String password,String photo,String phone,Integer sex){
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("phone", phone);
        List<FrontUser> phonesize = frontUserMapper.selectByMap(objectObjectHashMap);
        if (phonesize.size()==0) {
            String whole = secl.whole(LocalTime.now());
            String username=whole;
            Integer add = frontUserMapper.add(uname,username,password,photo,phone,sex);
            return add==1? Integer.valueOf(whole) :0;
        }
        return 2;
    }

    public List<Map<String,Object>> selectuser(Integer uid, Integer id){
        List<Map<String, Object>> seleuser = frontUserMapper.seleuser(uid, id);
        log.info(String.valueOf(seleuser));
        return seleuser;
    }

    public List<Map<String,Object>> attenuser(Integer uid){
        List<Map<String, Object>> attenuser = frontUserMapper.attenuser(uid);
        return attenuser;
    }

    public List<Map<String,Object>> fansuser(Integer uid){
       return frontUserMapper.fansuser(uid);
    }

    public List<Map<String,Object>> seleinform(Integer uid){
        List<Map<String, Object>> seleinform = frontUserMapper.seleinform(uid);
        for (Map<String, Object> map : seleinform) {
            if ((Integer) map.get("state")==0) {
                String st = (String) map.get("st");
                if (st.equals("1")) {
                    praiseMapper.updastate((Integer) map.get("cid"), 1);
                } else if (st.equals("3")) {
                    commentPraiseMapper.updastate((Integer) map.get("cid"), 1);
                } else if (st.equals("4") || st.equals("2")) {
                    commentMapper.updastate((Integer) map.get("cid"), 1);
                }
            }
        }
        return seleinform;
    }
}
